// var util = require('../util/util.js');
console.log('this is log/index.js');

var _util = require('util/util.js');


var _account_service = require('service/account-service.js');

var userLogin = {
    init : function(){
        $('#loginMsg').hide();
        this.bindEvents();
        return this;
    },
    bindEvents : function(){
        var _this=this;
        $('#submit').click(
            function(){
                _this.submit();
            }
        );
    },
    submit : function(){
        var formData ={
            username : $.trim($('#username').val()),
            password : $.trim($('#password').val())
        };
        console.log(formData);
        var validationResult = this.formValidation(formData);
        console.log(validationResult);
        if(validationResult.status){
            //向服务器发送请求
            _account_service.login(
                formData,
                function(res){
                    console.log("登陆成功");
                    window.location.href ='./catalog-main.html';
                },
                function(errMsg){
                    $('#loginMsg').show().text(errMsg);
                }
            );
        }else{
            $('#loginMsg').show().text(validationResult.msg);
        }
    },
    formValidation : function(formData){
        var result = {
            status :false,
            msg : ''
        };
        if( !_util.validateField('require',formData.username)){
            result.msg = '用户名不能为空';
            return result;
        }
        if( !_util.validateField('require',formData.password)){
            result.msg = '密码不能为空';
            return result;
        }
        //通过
        result.status = true;
        result.msg='通过';
        return result;
    }
};

module.exports = userLogin.init();